package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    //引用服务端
    @Reference
    private AddressService addressService;

    /**
     * 查询数据库中所有的坐标信息
     * @return
     */
    @RequestMapping("/findAddressAll")
    public Result findAddressAll(){

        try {
            //调用service层的方法查询数据库中的坐标
            List<Address> list = addressService.findAddressAll();

            System.out.println(list);

            //查询成功
            return new Result(true,"查询坐标信息成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询坐标信息失败");
        }
    /*
        List<Address> list = new ArrayList<>();

        Address add1 = new Address(114.308907, 30.550285, "黄鹤楼");
        Address add2 = new Address(114.408138,30.511611,"光谷广场大洋百货");
        Address add3 = new Address(114.419587,30.516527, "华中科技大学");

        list.add(add1);
        list.add(add2);
        list.add(add3);
    */
    }


    /**
     * 查询所有的坐标信息，然后分页查询，模糊查询
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            //调用service中的方法，进行分页查询
            PageResult pageResult = addressService.findPage(queryPageBean);

            //查询成功
            return new Result(true,"查询坐标信息成功",pageResult);

        } catch (Exception e) {
            e.printStackTrace();

            //查询失败
            return new Result(false,"查询坐标信息失败");
        }


    }



    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address address){

        //调用serivce层方法，添加坐标到数据库
        try {
            addressService.addAddress(address);

            //添加成功
            return new Result(true,"添加坐标成功");
        } catch (Exception e) {
            e.printStackTrace();

            //添加失败
            return new Result(false,"添加坐标失败");
        }
    }


    /**
     * 根据id删除坐标
     * @param id
     * @return
     */
    @RequestMapping("/delAddressById")
    public Result delAddressById(Integer id){

        try {
            addressService.delAddressById(id);

            return new Result(true,"删除坐标成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除坐标失败");
        }

    }
}
